/**
 * Project   : android-utility
 * Package   : org.basagee.activities
 * File name : TwitterLogin.java
 * Author    : Basagee
 * Date      : 2011. 4. 18.

 * Copyright
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.basagee.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author Basagee
 *
 */
public class TwitterLogin extends Activity {
	Intent mIntent;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twitter_login);
		
		WebView webView = (WebView)findViewById(R.id.webView);
		webView.setWebViewClient(new WebViewClient() {
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				
				Log.v(TwitterConst.TAG, "onPageFinished(), url == !!!" + url);
				// After logout, finish Activity
				if (url != null && url.equals("http://mobile.twitter.com/")) {
					setResult(RESULT_OK, mIntent);
					finish();
				} /*else if (url != null && url.startsWith("http://api.twitter.com/#!/logout")) {
					Log.v(TwitterConst.TAG, "Logout completed !!!!!");
					setResult(RESULT_OK, mIntent);
					finish();
				}*/ else if (url != null && url.startsWith(TwitterConst.TWITTER_CALLBACK_URL)) {
					String[] urlParameters = url.split("\\?")[1].split("&");
					String oauthToken = "";
					String oauthVerifier = "";
					
					try {
						if (urlParameters[0].startsWith("oauth_token"))
							oauthToken = urlParameters[0].split("=")[1];
						else if (urlParameters[1].startsWith("oauth_token"))
							oauthToken = urlParameters[1].split("=")[1];
						
						if (urlParameters[0].startsWith("oauth_verifier"))
							oauthToken = urlParameters[0].split("=")[1];
						if (urlParameters[1].startsWith("oauth_verifier"))
							oauthToken = urlParameters[1].split("=")[1];
						
						mIntent.putExtra("oauth_token", oauthToken);
						mIntent.putExtra("oauth_verifier", oauthVerifier);
						
						setResult(RESULT_OK, mIntent);
						finish();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		
		mIntent = getIntent();
		String url = mIntent.getStringExtra("auth_url");
		webView.loadUrl(url);
	}
}
